package com.bright.service.serviceimpl;

import com.bright.dao.BlogDao;
import com.bright.pojo.Blog;
import com.bright.pojo.Type;
import com.bright.service.BlogService;
import com.bright.vo.BlogQuery;
import com.bright.vo.BlogVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogDao blogDao;

    @Transactional
    @Override
    public Blog getBlog(long id) {
        return blogDao.getOne(id);
    }

    @Transactional
    @Override
    public Page<Blog> initBlogs(Pageable pageable) {
        return blogDao.findAll(pageable);
    }


    @Override
    public List<Blog> listBlogsPublished(Integer size) {
        Pageable pageable = PageRequest.of(0,size, Sort.by(Sort.Direction.ASC,"id"));
        return blogDao.findTop(pageable);
    }

    @Override
    public List<Blog> listBlogQuery(String query) {
        List<Blog> blogs = blogDao.findByQuery("%"+query+"%");
        return blogs;
    }

    @Transactional
    @Override
    public Page<Blog> getBlogs(Pageable pageable, BlogVo blogVo) {
        return blogDao.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                if (!"".equals(blogVo.getTitle()) && blogVo.getTitle()!=null){
                    predicates.add(cb.like(root.<String>get("title"),blogVo.getTitle()));
                }
                if (blogVo.getId()!=0){
                    predicates.add(cb.equal(root.<Type>get("type").get("id"),blogVo.getId()));
                }
                if (blogVo.isIsrecommand()){
                    predicates.add(cb.equal(root.<Boolean>get("isrecommend"),blogVo.isIsrecommand()));
                }
                cq.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        },pageable);
    }

    @Transactional
    @Override
    public Blog saveBlog(Blog blog) {
        if (blog == null){
            blog.setCreateTime(new Date());
            blog.setUpdateTime(new Date());
            blog.setViews(0);
        }else {
            blog.setUpdateTime(new Date());
        }
        return blogDao.save(blog);
    }

    @Transactional
    @Override
    public Blog updateBlog(long id, Blog blog) {
        Blog b = blogDao.getOne(id);
        if (b != null){
            blog.setUpdateTime(new Date());
            System.out.println(blog.getTags());
            System.out.println(blog.getType());
            return blogDao.save(blog);
        }
        return null;
    }

    @Transactional
    @Override
    public void deleteBlog(long id) {
        blogDao.deleteById(id);
    }

    @Override
    public void updateviews(long id) {
        Blog blog = blogDao.getOne(id);
        if (blog.getViews()==null){
            blog.setViews(0);
        }
        Integer count = blog.getViews();
        blog.setViews(count+1);
        blogDao.save(blog);
    }
}
